package com.jubaopen.service;

import java.util.List;
import com.jubaopen.bean.Account;
import com.jubaopen.dao.AccountDAO;

public class AccountService {

	private AccountDAO accountDAO;

	public void setAccountDAO(AccountDAO accountDAO) {
		this.accountDAO = accountDAO;
	}

	public int addAccount(Account account) throws Exception {
		return accountDAO.add(account);
	}

	public int updateAccount(Account account) throws Exception {
		return accountDAO.update(account);
	}

	public Account queryByAccountNum(String accountNum) {
		return accountDAO.getOneByAccountNum(accountNum);
	}

	public List<Account> queryListByUserId(int userId) {
		return accountDAO.getListByUserId(userId);
	}

	public boolean save(Account account) {
		List<Account> entryList = accountDAO.getListByUserId(account
				.getUserId());
		int ret = 0;

		if (entryList.size() > 0) {
			Account entry = entryList.get(0);
			account.setId(entry.getId());
			ret = accountDAO.update(account);
		} else {
			ret = accountDAO.add(account);
		}
		return ret > 0 ? true : false;
	}
}
